package pers.cz.javacvdemo.utils;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@Slf4j
public class ReflectUtils {

	public static Method getMethod(Class<?> clz, String name) {
		assert clz != null;
		assert name != null;
		Class<?> objClass = clz;
		while (objClass != null) {
			Method[] declaredMethods = objClass.getDeclaredMethods();
			for (Method declaredMethod : declaredMethods) {
				if (declaredMethod.getName().equals(name)) {
					return declaredMethod;
				}
			}
			objClass = objClass.getSuperclass();
		}
		log.error("{} not find method {}", clz.getName(), name);
		return null;
	}

	public static Object invoke(Object obj, String name, Object... args)
			throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
		Method method = getMethod(obj.getClass(), name);
		if (method == null) {
			throw new NoSuchMethodException(obj.getClass() + " not find method " + name);
		}
		method.setAccessible(true);
		return method.invoke(obj, args);
	}

	public static boolean isValueType(Class<?> cls) {
		try {
			return "java.lang.String".equals(cls.getName()) || cls.isPrimitive() || ((Class<?>) cls.getField("TYPE").get(null)).isPrimitive();
		} catch (IllegalAccessException | NoSuchFieldException e) {
			return false;
		}
	}
}
